package cn.xdf.demo01;

import cn.xdf.entity.User;
import org.junit.Test;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class Demo01_入门程序 {

    @Test
    public void test01(){

        //创建实体类管理器工厂对象
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("dev");//持久化单元

        //创建实体管理器
        EntityManager entityManager=entityManagerFactory.createEntityManager();

        //获取事务管理器
        EntityTransaction transaction = entityManager.getTransaction();

        //开启事务
        transaction.begin();

        //加上一条记录
        User user=new User(null,"xiaohui","2000-10-10","男","广州");
        entityManager.persist(user);

        //提交事务
        transaction.commit();

        entityManager.close();
        entityManagerFactory.close();


    }
}
